package com.jcfk.eam.controller.csm;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.csm.CsmBindEquipmentDTO;
import com.jcfk.eam.domain.query.csm.CsmBindEquipmentQuery;
import com.jcfk.eam.domain.uploadExcel.csm.CsmBindEquipmentUploadExcel;
import com.jcfk.eam.service.csm.CsmBindEquipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 耗材设备绑定
 *
 * @author yrl
 * @email
 * @date 2024-11-05
 */
@Api(tags = {"耗材设备绑定管理"})
@RestController
@RequestMapping("csmbindequipment")
public class CsmBindEquipmentController {
    @Autowired
    private CsmBindEquipmentService csmBindEquipmentService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<CsmBindEquipmentQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询耗材设备绑定")
    @PostMapping("/queryByPage")
    public RetPage<CsmBindEquipmentDTO> queryByPage(@RequestBody ReqPageParam<CsmBindEquipmentQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<CsmBindEquipmentQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<CsmBindEquipmentDTO> page = new DataPage<CsmBindEquipmentDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            CsmBindEquipmentDTO.class);
        page = csmBindEquipmentService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  耗材设备绑定
     * 根据id 查询详情
     */
    @ApiOperation("查询耗材设备绑定")
    @GetMapping("/get/{id}")
    public RetResult<CsmBindEquipmentDTO> get(@PathVariable("id") String id) {
            CsmBindEquipmentDTO item = csmBindEquipmentService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理耗材设备绑定
     */
    @ApiOperation("保存耗材设备绑定")
    @PostMapping("/save")
    @RequestLogHeader(modular = "耗材设备绑定管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid CsmBindEquipmentDTO csmBindEquipment) {
        // 数据校验
        RetResult ret = csmBindEquipmentService.isValid(csmBindEquipment);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(csmBindEquipment.getId())){
            // 新增
            csmBindEquipmentService.insert(csmBindEquipment);
        } else{
            // 更新
            csmBindEquipmentService.update(csmBindEquipment);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除耗材设备绑定
     */
    @ApiOperation("批量删除耗材设备绑定")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "耗材设备绑定管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody CsmBindEquipmentDTO csmBindEquipment) {
        return RetResult.ok(csmBindEquipmentService.logicDeleteList(csmBindEquipment));
    }



    /**
     * 导入耗材设备绑定
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<CsmBindEquipmentUploadExcel> items = EasyExcel.read(file.getInputStream(),CsmBindEquipmentUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        csmBindEquipmentService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<CsmBindEquipmentUploadExcel> list = new ArrayList<>();
        list.add(new CsmBindEquipmentUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, CsmBindEquipmentUploadExcel.class);
    }


    /**
     *  删除耗材设备绑定
     */
    @ApiOperation("删除耗材设备绑定")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "耗材设备绑定管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        csmBindEquipmentService.deleteItem(id);
        return RetResult.ok();
    }
}
